package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;

public class _163BbsAnalyse implements AnalyseNews {
	Map<String,String> map = new HashMap<String,String>();
	public _163BbsAnalyse(){
		map.put("http://bbs.local.163.com/", "网易论坛-北京");
		map.put("http://bbs.local.163.com/list/bj4hy.html", "网易论坛-北京");
		map.put("http://bbs.local.163.com/list/bjfb.html", "网易论坛-北京");
		map.put("http://bbs.local.163.com/list/bjpiao.html", "网易论坛-北京");
		map.put("http://bbs.local.163.com/list/bjtv.html", "网易论坛-北京");
		map.put("http://bbs.local.163.com/list/localms.html", "网易论坛-北京");

		map.put("http://bbs.local.163.com/list/localhenan.html", "网易论坛-河南");
		map.put("http://bbs.local.163.com/list/hnzt.html", "网易论坛-河南");
		map.put("http://bbs.local.163.com/list/hnscene.html", "网易论坛-河南");
		map.put("http://bbs.local.163.com/list/hnjkms.html", "网易论坛-河南");

		map.put("http://bbs.local.163.com/list/localgd.html", "网易论坛-广东");
		map.put("http://bbs.local.163.com/list/localsh.html", "网易论坛-上海");
		map.put("http://bbs.local.163.com/list/localsc.html", "网易论坛-四川");
		map.put("http://bbs.local.163.com/list/localjs.html", "网易论坛-江苏");
		map.put("http://bbs.local.163.com/list/localzj.html", "网易论坛-浙江");
		map.put("http://bbs.local.163.com/list/localhubei.html", "网易论坛-湖北");
		map.put("http://bbs.local.163.com/list/localhunan.html", "网易论坛-湖南");
		map.put("http://bbs.local.163.com/list/localfj.html", "网易论坛-福建");
		map.put("http://bbs.local.163.com/list/localah.html", "网易论坛-安徽");
		map.put("http://bbs.local.163.com/list/localhainan.html", "网易论坛-海南");
		map.put("http://bbs.local.163.com/list/localnmg.html", "网易论坛-内蒙古");
		map.put("http://bbs.local.163.com/list/localsd.html", "网易论坛-山东");
		map.put("http://bbs.local.163.com/list/localln.html", "网易论坛-辽宁");
		map.put("http://bbs.local.163.com/list/localhb.html", "网易论坛-河北");
		map.put("http://bbs.local.163.com/list/localsx.html", "网易论坛-山西");
		map.put("http://bbs.local.163.com/list/localtj.html", "网易论坛-天津");
		map.put("http://bbs.local.163.com/list/localjl.html", "网易论坛-吉林");
		map.put("http://bbs.local.163.com/list/localhlj.html", "网易论坛-黑龙江");
		map.put("http://bbs.local.163.com/list/localguizhou.html", "网易论坛-贵州");
		map.put("http://bbs.local.163.com/list/localshanxi.html", "网易论坛-陕西");
		map.put("http://bbs.local.163.com/list/localgs.html", "网易论坛-甘肃");
		map.put("http://bbs.local.163.com/list/localcq.html", "网易论坛-重庆");
		map.put("http://bbs.local.163.com/list/localgx.html", "网易论坛-广西");
		map.put("http://bbs.local.163.com/list/localyn.html", "网易论坛-云南");
		map.put("http://bbs.local.163.com/list/localxj.html", "网易论坛-新疆");
		map.put("http://bbs.local.163.com/list/localqh.html", "网易论坛-青海");
		map.put("http://bbs.local.163.com/list/localnx.html", "网易论坛-宁夏");
		map.put("http://bbs.local.163.com/list/localxz.html", "网易论坛-西藏");

	}

	public String getSourceName(String src_url ){
		String string = map.get(src_url);
		if(string!=null){
			return string;
		}
		return "网易论坛";
	}
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		// http://bbs.lady.163.com/bbs/lovestory/278999888.html
		// http://club2011.auto.163.com/post/100009836667.html
		//http://bbs.game.163.com/thread-171991474-1-1.html
		//http://club.tech.163.com/photoalbum/mobile_museum/259288475.html
		String[] regex = {
				"http://.*.163.com/bbs/.*/[0-9]*.html",
				"http://club[0-9]*.*.163.com/post/[0-9]*.html",
				"http://bbs.game.163.com/thread-[0-9]*-1-1.html",
				"http://.*.163.com/photoalbum/.*/[0-9]*.html",
				"http://.*.163.com/thread/[a-z]*-[0-9]*-[0-9|a-zA-Z]+.html",
				"http://digi.163.com/[0-9]*/[0-9]*/[0-9]*/.*.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum = 0;
		Integer commentNum = 0;
		String author=null;
		Document doc = Jsoup.parse(htmltxt);

		String _404Str = doc.select("iframe#pageiframe").attr("src");
		if(_404Str!=null && _404Str.contains("404_page")){
			bbs.setUpdateUrl(ParseState.ERR404.toString());
			return bbs;
		}


		if (url.matches("http://.*.163.com/bbs/.*/[0-9]*.html")) {
			title = doc.select("h2.tie-con-hd-title").text();
			content = HtmlCleaner.getContentHtml(url,  doc.select("div.tie-first-item div.tie-con div.tie-con-bd div.tie-content"));
			date = DateUtils
					.matchDate(doc
							.select(
									"div.tie-first-item div.tie-con div.tie-con-hd div.tie-con-hd-panel span.time")
							.text());
			/*
			 * 解析浏览数，点击数
			 */
			Elements select = doc
					.select("html body div#tie_bd div#tie_wrapper div.tie-tab-bottom-nav div.tie-page span.red");
			if (select.size() > 1) {
				String text = select.get(0).text();
				try {
					clickNum = Integer.parseInt(text);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
				String text1 = select.get(1).text();
				try {
					commentNum = Integer.parseInt(text1);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
			}
		} else if (url.matches("http://club[0-9]*.*.163.com/post/[0-9]*.html")) {
			title = doc.select("div#mainPost div.bbs_show_title div.bbs_title h3").text();
			content = HtmlCleaner.getContentHtml(url, doc.select(	"div.bbs_show_content"));
			date = DateUtils.matchDate(doc.select("div#mainPost div.bbs_show_list div.bbs_show_item div.bbs_show_main div.bbs_show_title div.bbs_time small")	.text());
			author=doc.select("div#mainPost .bbs_user_head a:eq(1)").text();
			/*
			 * 解析浏览数，点击数
			 */
			Elements select = doc
					.select(" div#bbs_show_top div.col-lm div.col-l span");
			if (select.size() > 1) {
				String text = select.get(0).text().replace("回复：", "");
				try {
					clickNum = Integer.parseInt(text);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
				String text1 = select.get(1).text().replace("查看：", "");
				;
				try {
					commentNum = Integer.parseInt(text1);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
			}
		}else if(url.matches("http://bbs.game.163.com/thread-[0-9]*-1-1.html")){
			title = doc.select("html body#nv_forum.pg_viewthread div.html table.laywidth tbody tr td.bg_color div#wp.wp div#ct.wp div#postlist.pl table tbody tr td.plc h1.ts a#thread_subject").text();
			String dateStr = doc.select("html body#nv_forum.pg_viewthread div.html table.laywidth tbody tr td.bg_color div#wp.wp div#ct.wp div#postlist.pl div#post_177092907 table#pid177092907 tbody tr td.plc div.pi div.pti div.authi em#authorposton177092907").text();
			date=DateUtils.matchDate(dateStr);
			if(date==null){
				dateStr = doc.select("html body#nv_forum.pg_viewthread div.html table.laywidth tbody tr td.bg_color div#wp.wp div#ct.wp div#postlist.pl div table tbody tr td.plc div.pi div.pti div.authi em span").attr("title");
				date= DateUtils.matchDate(dateStr);
			}
			content = HtmlCleaner.getContentHtml(url, doc.select("tbody tr td.plc div.pct div.pcb div.t_fsz table tbody tr td.t_f"));
			Elements select = doc.select("html body#nv_forum.pg_viewthread div.html table.laywidth tbody tr td.bg_color div#wp.wp div#ct.wp div#postlist.pl table tbody tr td.pls div.hm span.xi1");
			if (select.size() > 1) {
				String text = select.get(0).text();
				try {
					clickNum = Integer.parseInt(text);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
				String text1 = select.get(1).text();
				try {
					commentNum = Integer.parseInt(text1);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
			}
		}else if(url.matches("http://.*.163.com/photoalbum/.*/[0-9]*.html")){
			title=doc.select("html body div.nphbbs_wrap div.nphbbs_area div#gallery.nphbbs_gallery div.nphbbs_cnt div.nphbbs_set_info div#setInfo div.nphbbs_set_title h1").text();
			String html = doc.select("html head script").html();
			date = DateUtils.matchDate(html);
			String text = doc.select("html body div.nphbbs_wrap div.nphbbs_area div#gallery.nphbbs_gallery div.nphbbs_cnt div.nphbbs_set_info div#setInfo div.nphbbs_set_title span:eq(2)").text().replace("人查看", "");
			try {
				clickNum = Integer.parseInt(text);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
			}
			String text2 = doc.select("html body div.nphbbs_wrap div.nphbbs_area div.nphbbs_box_title span.nphbbs_comment_count a span.cDRed").text();
			try {
				commentNum = Integer.parseInt(text2);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
			}
		}else if(url.matches("http://digi.163.com/[0-9]*/[0-9]*/[0-9]*/.*.html")){
			title=doc.select("#h1title").text();		
			date = DateUtils.matchDate(doc.select(".ep-time-soure").text());
			content= HtmlCleaner.getContentHtml(url, doc.select("div.news_content"));			
		}
		else{
			title = doc.select("h2.tie-con-hd-title").text();
			content = HtmlCleaner.getContentHtml(url,  doc.select("div.tie-first-item div.tie-con div.tie-con-bd div.tie-content"));
			date = DateUtils
					.matchDate(doc
							.select(
									"div.tie-first-item div.tie-con div.tie-con-hd div.tie-con-hd-panel span.time")
							.text());
			/*
			 * 解析浏览数，点击数
			 */
			Elements select = doc
					.select("html body div#tie_bd div#tie_wrapper div.tie-tab-bottom-nav div.tie-page span.red");
			if (select.size() > 1) {
				String text = select.get(0).text();
				try {
					clickNum = Integer.parseInt(text);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
				String text1 = select.get(1).text();
				try {
					commentNum = Integer.parseInt(text1);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
			}
		}
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setDate(date);
		/**
		 * 解析用于更新的地址
		 */
		bbs.setUpdateUrl(url);

		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		if(author==null){
			Elements select = doc.select("div#tie_bd div#tie_wrapper div.tie-item div.tie-author a.js-tieauthor");
			if(select!=null && select.size()!=0){
				author=select.get(0).text();
			}
		}
		bbs.setAuthor(author);

		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta != null) {
			String updateUrl = meta.getUpdateUrl();
			if (updateUrl != null) {
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if (responseToURL != null) {
					String html = responseToURL.getHtml();
					if (html != null) {
						Document doc = Jsoup.parse(html);
						if (updateUrl
								.matches("http://.*.163.com/bbs/.*/[0-9]*.html")) {
							Elements select = doc
									.select("html body div#tie_bd div#tie_wrapper div.tie-tab-bottom-nav div.tie-page span.red");
							if (select.size() > 1) {
								try {
									String text = select.get(0).text();
									meta.setClickNum(Integer.parseInt(text));
									String text1 = select.get(1).text();
									meta.setCommentNum(Integer.parseInt(text1));
									return meta;
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
								}

							}
						}else if (updateUrl.matches("http://club[0-9]*.*.163.com/post/[0-9]*.html")) {
							Elements select = doc
							.select("html body div.uc_body div#uc_wrap.uc_wrap div#bbs_show_top.mod div.col-lm div.col-l span");
							if (select.size() > 1) {
								try {
									String text = select.get(0).text().replace("回复：", "");;
									meta.setClickNum(Integer.parseInt(text));
									String text1 = select.get(1).text().replace("查看：", "");;
									meta.setCommentNum(Integer.parseInt(text1));
									return meta;
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
								}

							}
						}else if (updateUrl
								.matches("http://bbs.game.163.com/thread-[0-9]*-1-1.html")) {
							Elements select = doc.select("html body#nv_forum.pg_viewthread div.html table.laywidth tbody tr td.bg_color div#wp.wp div#ct.wp div#postlist.pl table tbody tr td.pls div.hm span.xi1");
							if (select.size() > 1) {
								try {
									String text = select.get(0).text();
									meta.setClickNum(Integer.parseInt(text));
									String text1 = select.get(1).text();
									meta.setCommentNum(Integer.parseInt(text1));
									return meta;
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
								}

							}
						}else if(updateUrl.matches("http://.*.163.com/photoalbum/.*/[0-9]*.html")){
							String text = doc.select("html body div.nphbbs_wrap div.nphbbs_area div#gallery.nphbbs_gallery div.nphbbs_cnt div.nphbbs_set_info div#setInfo div.nphbbs_set_title span:eq(2)").text().replace("人查看", "");
							String text2 = doc.select("html body div.nphbbs_wrap div.nphbbs_area div.nphbbs_box_title span.nphbbs_comment_count a span.cDRed").text();
							try {
								meta.setClickNum(Integer.parseInt(text));
								meta.setCommentNum(Integer.parseInt(text2));
								return meta;
							} catch (NumberFormatException e) {
								// TODO Auto-generated catch block
							}
						}
					}
				}
			}
		}
		return null;
	}

	public static void main(String[] args) {
		_163BbsAnalyse a = new _163BbsAnalyse();
		String url = "http://digi.163.com/15/0309/13/AK93D8IS001668V5.html";

		if (!a.isDetailPage(url)) {
			System.out.println("不匹配规则");
		} else {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		}

	}
	
}
